<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetRenxuanDeta, PatchModifyType } from '@/api/modules/renxuan'
// import PageNumber from '@/store/modules/PageNumberCache'
import GetCacheData from '@/store/modules/NewPageCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
// const PageNumberCache = PageNumber()
const cacheData = GetCacheData()

// 查询表单
const findFrom = ref({
  ...(cacheData.GetCacheData('renxuan').fromData),
})

const dataList: any = reactive({
  list: [],
})

const loading = ref(true)
const total = ref(0)
let PageNum = cacheData.GetCacheData('renxuan').currentPage
const options = ref([
  { value: 0, label: '未开始' },
  { value: 1, label: '进行中' },
  { value: 2, label: '已结束' },
])

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.Title && { Title: findFrom.value.Title }),
    ...((findFrom.value.ActStatus as number | string) !== '~' && { ActStatus: findFrom.value.ActStatus }),
  }
  await GetRenxuanDeta(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('renxuan').currentSize)
  cacheData.SetCacheData('renxuan', PageNum, cacheData.GetCacheData('renxuan').currentSize, findFrom.value)
}

// 修改状态
function ModifyType(params: any) {
  PatchModifyType(params).then((res: any) => {
    if (res.statusCode === 200) {
      ElMessage.success('修改成功')
      GetDataList(PageNum, cacheData.GetCacheData('renxuan').currentSize)
    }
    else {
      ElMessage.error(`修改失败${res.errors}`)
    }
  })
}

// 添加
function AddData() {
  router.push({
    name: 'renxuanDetail',
  })
}

function PageJumps(routerName: string, query?: any) {
  router.push({
    name: routerName,
    query,
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('renxuan', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('renxuan').currentSize)
  cacheData.SetCacheData('renxuan', val, cacheData.GetCacheData('renxuan').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('renxuan').currentPage || 1
})

onMounted(async () => {
  GetDataList(PageNum, cacheData.GetCacheData('renxuan').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="标题">
                <el-input v-model="findFrom.Title" placeholder="请输入标题" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="活动状态">
                <el-select v-model="findFrom.ActStatus" placeholder="请选择状态">
                  <el-option label="全部" value="~" />
                  <el-option label="未开始" :value="0" />
                  <el-option label="进行中" :value="1" />
                  <el-option label="已结束" :value="2" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="rxActId" label="活动ID" width="100" />
        <el-table-column align="center" width="200" prop="title" label="标题" />
        <el-table-column align="center" width="200" prop="amount" label="价格" />
        <el-table-column align="center" width="200" prop="nums" label="任选件数" />
        <el-table-column align="center" prop="banner" label="banner图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.banner" />
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" prop="zengpinTitle" label="赠品标题" />
        <el-table-column align="center" prop="zengpinPic" label="赠品图片" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.zengpinPic" />
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" prop="zengpinCode" label="赠品编码" />
        <el-table-column header-align="center" width="300" prop="shareTxt" label="分享语" />
        <el-table-column align="center" prop="shareImg" label="分享图" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="imgDomain + scope.row.shareImg" />
          </template>
        </el-table-column>
        <el-table-column header-align="center" width="300" prop="desp" label="描述" />
        <el-table-column align="center" width="240" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="240" prop="updateTime" label="更新时间" />
        <el-table-column align="center" label="状态" fixed="right" width="240">
          <template #default="scope">
            <el-segmented
              v-model="scope.row.actStatus"
              :options="options"
              @change="ModifyType({ rxActId: scope.row.rxActId, actStatus: scope.row.actStatus })"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="200">
          <template #default="scope">
            <div style="line-height: 42px;">
              <el-button type="success" @click="PageJumps('renxuanProductList', { RxActId: scope.row.rxActId })">
                关联商品
              </el-button>
              <el-button type="primary" @click="PageJumps('renxuanDetail', { id: scope.row.rxActId })">
                编辑
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('renxuan').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }

  @media screen and (width <= 500px) {
    .from {
      grid-template-columns: repeat(auto-fill, minmax(30vw, 1fr));
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}
</style>
